use strict;

sub files_to_rm
{
 	my ($file_name,$temp)=@_;
	my $big_ct_file = $file_name."_".$temp."\.ct";
	`rm -f $big_ct_file`;
	`rm -f *.sav`;		
	`rm -f *.rnaml`;		
	`rm -f *.plot`;		
	`rm -f *.sav`;		
	`rm -f *.ps`;		
	`rm -f *.out`;		
}

sub run_mfold
{
    my ($itemp, $ftemp, $fname) = @_;
    print "run_mfold invoked with $itemp, $ftemp, $fname\n";
    for(my $temp = $itemp; $temp <= $ftemp; $temp++)
    {
        print "Processing at temperature: $temp \n";
        mkdir $temp;
        chdir $temp;
        my $new_file_name=$fname."_".$temp."\.seq";
        `cp -f ../$fname".seq" ./$new_file_name`;
        my @result = `mfold SEQ=$new_file_name T=$temp 2>&1`;
        my $output_file=$fname."_"."mfold_output\.txt";
        open(OUT, ">$output_file") or warn "can not open output file: $!";
        print OUT @result;

     	files_to_rm($fname,$temp);
		close(OUT);
         chdir "..";
    }

    return;
}



sub pack_results
{
    my ($itemp, $ftemp, $fname,$result_name,$complementary)= @_;
    print "Packing the Resutls and foldings\n";
	my $files_arguments=$result_name;
    for(my $temp = $itemp; $temp <= $ftemp; $temp++)
	{
		 $files_arguments=join(" ",$files_arguments,$temp);
	}
	if (defined ($complementary))
	{
		$files_arguments=join(" ",$files_arguments,$fname."_compl\.seq");
	}
	my $tar_name=$fname."\.tgz";
	my $tar_cmd="tar -zcf";
	my $rm_cmd="rm -rf";
	`$tar_cmd $tar_name $files_arguments`;
	`$rm_cmd $files_arguments`;
}


sub make_complementary
{
my ($file_name,$file_name_comple)=@_;

my %complem = ( "a" => "t",
		"t" => "a",
		"c" => "g",
		"g" => "c",
		" " => " "
 		);

$file_name.=".seq";
$file_name_comple.=".seq";

my $HANDLE;
my $HANDLECOMPL;
open_file($file_name,\$HANDLE);
open_file(">$file_name_comple",\$HANDLECOMPL);

my $charCount=0;
my $lineNucleotidos="";
while (read($HANDLE,my $char,1))
{	
	if ($char =~ m/\b(a|t|c|g)/i)
  	{ 
	$lineNucleotidos=join('',$complem{lc($char)},$lineNucleotidos);
	$charCount++;
		if($charCount == 60)
		{
			$charCount=0;
			$lineNucleotidos=join('',"\n",$lineNucleotidos);
			
		}	
	}
}
print $HANDLECOMPL "$lineNucleotidos\n";
close($HANDLE);
close($HANDLECOMPL);
return;
}
###############################

sub read_nucleotidos_apareaos
{
my ($file_name,$returnValue,$iniNucleo,$endNucleo)=@_;

my $HANDLE;
open_file($$file_name,\$HANDLE);

    my $DIGITS="\(\\d{1,5}\)";
    my $PATTER="\^\\s+".$DIGITS."\\s+\\w\\s+".$DIGITS."\\s+".$DIGITS."\\s+".$DIGITS."\\s+".$DIGITS;
	
    my $countApp=0;
    my $nucleo=1;
    my @line=("");
	
	while ( defined(local $_= <$HANDLE>) && ($nucleo != $endNucleo))
	{
 	        if(m/$PATTER/)
		{
	  	 $nucleo=$1;
	  	 my $appare=$4;
   	  	 if($nucleo >= $iniNucleo)
 	    	 {
		     	if ($appare != 0)
	      		{
			 $countApp++;
			}
		  }	
	    	}
		 
	}

	close ($HANDLE);

 	$$returnValue=$countApp;
	return ;
}

##############################################################
sub get_energy{
my ($det_file,$energy,$init_zone,$end_zone,$not_run_parser_det)=@_;

    unless(defined ($not_run_parser_det))
    {
	my $program_parser="./det_parser";	
	my $struct_number=1;
	
	my @output = `$program_parser $$det_file $struct_number $init_zone $end_zone`;
	chop @output;
	my @newOut = split /\:/,$output[0];
	$newOut[1]+=0; # Casting to number;
	$$energy=$newOut[1];
    }
}
#########################################3

sub sum_zone_no_apareados
{

	my ($file_name,$returnValue,$iniNucleo,$endNucleo)=@_;
	
	my $HANDLE;
	open_file($$file_name,\$HANDLE);
# 	open (HANDLE, $$file) || die "can't opendir $namefile: $!";
	my $DIGITS="\(\\d{1,5}\)";
	my $PATTER="\^\\s+".$DIGITS."\\s+\(\\d{1,3}\)\\s+\\w{1}\$";		
	my $PATTESTRUCT="^\\s+\(\\d{1,3}\)\$";	
	my $addNotApp=0;
	my $nucleo=1;
	my $number_struct=0;
	while ( defined(local $_= <$HANDLE>) && ($nucleo != $endNucleo))
	{
 	        if(m/$PATTER/)
		{
	  	 $nucleo=$1;
	  	  my $appare=$2;
   	  	  if($nucleo >= $iniNucleo)
 	    	   {
		     $addNotApp+=$appare;	
		   }	
	    	}elsif (m/$PATTESTRUCT/) 
		{
		 	$number_struct=$1;
		}
		 
	}
	
 close ($HANDLE);
 if($endNucleo == 0 ){ $endNucleo=$nucleo;}
 my $zone= ($endNucleo-$iniNucleo+1) * $number_struct;
 my $output= ($addNotApp)/$zone;
 $$returnValue=$output;
return ;
}
########################################

sub open_file
{
use Carp;

 my ($file_name,$HANDLE)=@_;
  	open($$HANDLE,$file_name) || carp "can't opendir $file_name: $!";
return ;
}

	
	sub get_data_header_file
{
	my ($file_name)=@_;
	
	my $HANDLE;
	open_file($file_name,\$HANDLE);

# 	3411 dG =  -1266.90 junin_s_24   
	$_=<$HANDLE>;
	close($HANDLE);
	s/^\s+|\s+$//g;
	s/\s+/ /g;
	my @data_header= split/\s/,$_; 
	$data_header[3]+=0;
	return ($data_header[0],$data_header[3]);
}

return 1;
